/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.bean.TableCell;
import com.jb.web.bean.TableHeader;
import com.jb.web.bean.UIComponentImpl;
import com.jb.web.context.WebContext;

import java.util.Iterator;

import com.jb.faces.component.UIComponent;


public class TableCellRenderer extends TextRenderer {
    public StringBuffer renderBegin(WebContext pCtx, 
                                    UIComponent itemToRender) {
        TableCell tableData = (TableCell)itemToRender;
        String style = tableData.getStyle(pCtx);
        String styleClass = tableData.getStyleClass();
        StringBuffer html = new StringBuffer();

        if (tableData.getParent() instanceof TableHeader) {
            html.append("<TH ");
        } else {
            html.append("<TD ");
        }

        if (tableData.getColSpan() > 1)
            html.append(addTagProperty("colspan", "" + tableData.getColSpan(), 
                                       null));
        if (tableData.getRowSpan() > 1)
            html.append(addTagProperty("rowspan", "" + tableData.getRowSpan(), 
                                       null));

        html.append(addTagProperty("align", tableData.getAlign(), null));
        html.append(addTagProperty("valign", tableData.getVerticalAlign(), 
                                   null));
        html.append(addTagProperty("style", style, null))
            .append(addTagProperty("class", styleClass,null));

        html.append(" ").append(getEditScript(pCtx, tableData));
        html.append(">");

        return html;
    }

    public StringBuffer renderChildren(WebContext pCtx, 
                                       UIComponent itemToRender) {
        TableCell tableData = (TableCell)itemToRender;
        StringBuffer html = new StringBuffer();
        Iterator iter = tableData.getFacetsAndChildrenSorted(null).iterator();
        boolean childrenPainted = false;

        while (iter.hasNext()) {
            UIComponentImpl aSect = (UIComponentImpl)iter.next();

            if (isVisible(pCtx, aSect)) {
                html.append(aSect.render(pCtx));
                childrenPainted = true;
            }
        }

        if (!childrenPainted) {
            String strValue = getValue(pCtx, tableData);
            html.append(isEmptyString(strValue) ? "" : strValue);
        }
        return html;
    }

    public StringBuffer renderEnd(WebContext pCtx, UIComponent itemToRender) {
        StringBuffer html = new StringBuffer("");
        TableCell cell = (TableCell)itemToRender;

        if (cell.getParent() instanceof TableHeader) {
            html.append("</TH >");
        } else {
            html.append("</TD >");
        }
        return html;
    }
}
